package com.wr.springbootsss.controller;

import com.wr.springbootsss.pojo.LoginResult;
import com.wr.springbootsss.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author 王瑞
 * @description: 登录页面的controller
 * @date 2020/3/10  21:57
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    @RequestMapping("/toLogin")
    public String test(){
        return "login";
    }

    @RequestMapping("/login")
    @ResponseBody
    public LoginResult login(@RequestBody User user, HttpSession session){

        //返回页面的pojo 用来返回当前用户是否合法的信息
        LoginResult loginResult = new LoginResult();
        //获得用户输入的用户名和密码
        String username = user.getUsername();
        String password = user.getPassword();

        //判断用户名和密码
        if ("admin".equals(username) && "admin".equals(password)){
            loginResult.setIsOk(true);
            //如果账号密码没问题  就把用户保存到session中
            session.setAttribute("user",user);
        }else {
            loginResult.setIsOk(false);
            loginResult.setMsg("用户名或者密码错误，请重新输入~！");
        }

        return loginResult;

    }
}
